﻿using System;
using System.Configuration;
using mojoPortal.Net;
using mojoPortal.Web;
using Resources;
using mojoPortal.Web.Framework;
using System.Collections;
using mojoPortal.Business;
using ud_gSDMS.Code_Service;
using mojoPortal.Business.WebHelpers;
using System.IO;
using Brettle.Web.NeatUpload;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI;
namespace ud_gSDMS.Member
{
    public partial class Training : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        private string id;
        private SiteUser _siteUser;
        protected String cacheDependencyKey;
        DBQLNS _respository=new DBQLNS();
        private string lstNganhDT;
        private string lstHinhthucDT;
        private string lstBangCap;
        void Page_Load(object sender, EventArgs e)
        {
            _siteUser = SiteUtils.GetCurrentSiteUser();
            if (_siteUser == null)
            {
                WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
            }

            if (!Page.IsPostBack)
            {
                PopulateLabels();
                PopulateControls();
            }
        }
        private void PopulateLabels()
        {
            leftMenuItem.CurrentSite = Request.RawUrl;
            
           
        }
        private void BindData()
        {
            List<ud_QTDT> lst = new List<ud_QTDT>();
            lst = _respository.getAllQTDTByUserID(1);
            gvChildInfo.DataSource = lst;
            gvChildInfo.DataBind();
        }

        private void PopulateControls()
        {
            BindData();
        }
        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
            Load += Page_Load;
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);
            SuppressPageMenu();
        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);
            id = WebUtils.ParseStringFromQueryString("id", id);
            cacheDependencyKey = "Module-" + moduleId.ToInvariantString();
        }

        private void LoadPanels()
        {
            bool showLeftColumnAdmin = ConfigHelper.GetBoolProperty("ShowLeftColumnAdmin", false);
            bool showRightColumnAdmin = ConfigHelper.GetBoolProperty("ShowRightColumnAdmin", false);
            bool showTopColumnAdmin = ConfigHelper.GetBoolProperty("ShowTopColumnAdmin", false);
            bool showBottomColumnAdmin = ConfigHelper.GetBoolProperty("ShowBottomColumnAdmin", false);
            int showCenterColumnAdmin = ConfigHelper.GetIntProperty("ShowCenterColumnAdmin", 0);
            LoadSideContent(showLeftColumnAdmin, showRightColumnAdmin, showTopColumnAdmin, showBottomColumnAdmin, showCenterColumnAdmin);
        }

        protected void nganhDT_Load(object sender, EventArgs e)
        {

            DropDownList dr = (DropDownList)sender;

            dr.DataValueField = "ID";
            dr.DataTextField = "Name";
            List<ud_DM_NganhDT> lstNganh = _respository.getAllNganhDT();
            dr.DataSource = lstNganh;

            dr.DataBind();

            //for (int i = 0; i < dr.Items.Count; i++)
            //{
            //    if (dr.Items[i].Text == lstNganhDT)
            //    {
            //        dr.SelectedIndex = i;
            //        break;
            //    }
            //}
            
        }
        protected void hinhthucDT_Load(object sender, EventArgs e)
        {

            DropDownList dr = (DropDownList)sender;

            dr.DataValueField = "ID";
            dr.DataTextField = "Name";
            List<ud_DM_HinhThucDT> lstHinhthuc = _respository.getAllHinhthucDT();
            dr.DataSource = lstHinhthuc;

            dr.DataBind();

            //for (int i = 0; i < dr.Items.Count; i++)
            //{
            //    if (dr.Items[i].Text == lstHinhthucDT)
            //    {
            //        dr.SelectedIndex = i;
            //        break;
            //    }
            //}
        }
        protected void bangcap_Load(object sender, EventArgs e)
        {

            DropDownList dr = (DropDownList)sender;

            dr.DataValueField = "ID";
            dr.DataTextField = "Name";
            List<ud_DM_BangCap> lstBang = _respository.getAllBangCap();
            dr.DataSource = lstBang;

            dr.DataBind();

            //for (int i = 0; i < dr.Items.Count; i++)
            //{
            //    if (dr.Items[i].Text == lstBangCap)
            //    {
            //        dr.SelectedIndex = i;
            //        break;
            //    }
            //}
        }
        #endregion

        protected void gvChildInfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            lstNganhDT = ((Literal)gvChildInfo.Rows[e.NewEditIndex].FindControl("ltrNganhDT")).Text;
            lstHinhthucDT = ((Literal)gvChildInfo.Rows[e.NewEditIndex].FindControl("ltrHinhThucDT")).Text;
            lstBangCap = ((Literal)gvChildInfo.Rows[e.NewEditIndex].FindControl("ltrBangDuocCap")).Text;
            gvChildInfo.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void gvChildInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvChildInfo.EditIndex = -1;
            BindData();
        }

        protected void gvChildInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            
            TextBox txt_nhaphoc = (TextBox)gvChildInfo.Rows[e.RowIndex].FindControl("txt1");
            TextBox txt_totnghiep = (TextBox)gvChildInfo.Rows[e.RowIndex].FindControl("txt2");
            TextBox txt_noidt = (TextBox)gvChildInfo.Rows[e.RowIndex].FindControl("txt3");
            DropDownList txt_nganhdt = (DropDownList)gvChildInfo.Rows[e.RowIndex].FindControl("drdl_nganhDT");
            DropDownList txt_hinhthucdt = (DropDownList)gvChildInfo.Rows[e.RowIndex].FindControl("drdl_hinhthucDT");
            DropDownList txt_bangduoccap = (DropDownList)gvChildInfo.Rows[e.RowIndex].FindControl("drdl_bangcap");
            int gridid = int.Parse(((HiddenField)gvChildInfo.Rows[e.RowIndex].FindControl("ObjectID")).Value);
            int test1 = int.Parse(txt_nganhdt.SelectedItem.Value);
            string test2 = txt_nganhdt.SelectedItem.Text;
            int test4= int.Parse(txt_nganhdt.SelectedValue);
            int test5 = int.Parse(txt_nganhdt.SelectedIndex.ToString());
            ud_QTDT newob = new ud_QTDT { UserID=1, NhapHoc=txt_nhaphoc.Text, TotNghiep=txt_totnghiep.Text, NoiDTBD=txt_noidt.Text,
                                DM_NganhDT=int.Parse(txt_nganhdt.SelectedItem.Value), DM_HinhThucDT=int.Parse(txt_hinhthucdt.SelectedItem.Value), DM_BangCap=int.Parse(txt_bangduoccap.SelectedValue), Active=true, UpdatedDate=DateTime.Now};

            _respository.updateQTDT(gridid, newob);
            gvChildInfo.EditIndex = -1;
            BindData();
        }

        protected void gvChildInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            List<TrainingP> l = (List<TrainingP>)Session["Tr"];
            l.RemoveAt(e.RowIndex);
            BindData();
        }
    }

    public class TrainingP
    {
        public string NhapHoc { get; set; }
        public string TotNghiep { get; set; }
        public string NoiDT { get; set; }
        public string NganhDT { get; set; }
        public string HinhThucDT { get; set; }
        public string BangDuocCap { get; set; }
        public TrainingP(string nhapHoc, string totNghiep, string noiDT, string nganhDT, string hinhThucDT, string bangDuocCap)
        {
            this.NhapHoc = nhapHoc;
            this.TotNghiep = totNghiep;
            this.NoiDT = noiDT;
            this.NganhDT = nganhDT;
            this.HinhThucDT = hinhThucDT;
            this.BangDuocCap = bangDuocCap;
        }
    }

    
}